package com.ewzj.bigdata.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ewzj.bigdata.R;
import com.ewzj.bigdata.app.constants.AppConstants;
import com.ewzj.bigdata.di.component.DaggerSearchResultComponent;
import com.ewzj.bigdata.di.module.SearchResultModule;
import com.ewzj.bigdata.mvp.contract.SearchResultContract;
import com.ewzj.bigdata.mvp.model.entity.resbean.NewBean;
import com.ewzj.bigdata.mvp.model.entity.resbean.SearchResultBean;
import com.ewzj.bigdata.mvp.presenter.SearchResultPresenter;
import com.ewzj.bigdata.mvp.ui.adapter.NewsAdapter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class SearchResultActivity extends BaseActivity<SearchResultPresenter> implements SearchResultContract.View {


    @BindView(R.id.rv_searchs)
    RecyclerView rvSearchs;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    private SearchResultBean searchResultBean;
    private List<NewBean> newBeans;
    private NewsAdapter newsAdapter;
    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerSearchResultComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .searchResultModule(new SearchResultModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(Bundle savedInstanceState) {
        return R.layout.activity_search_result; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        searchResultBean=getIntent().getParcelableExtra(AppConstants.EXTRA_DATA);
        if (searchResultBean==null){
            finish();
        }
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.loadInitData();
            }
        });
        init();
        mPresenter.loadInitData();
    }

    private void init() {
        if (newBeans==null){
            newBeans=new ArrayList<>();
        }
        if (newsAdapter==null){
            newsAdapter=new NewsAdapter(newBeans);
        }
        rvSearchs.setLayoutManager(new LinearLayoutManager(this));
        rvSearchs.setAdapter(newsAdapter);
        newsAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.loadMoreData();
            }
        },rvSearchs);

    }


    @Override
    public void showLoading() {
        swipeRefreshLayout.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void refreshAdapter(List<NewBean> newBeans) {
        newsAdapter.setNewData(newBeans);
    }

    @Override
    public void notifyAddData(List<NewBean> newBeans) {
        newsAdapter.addData(newBeans);
    }

    @Override
    public SearchResultBean obtainSearchResultBean() {
        return searchResultBean;
    }

    @Override
    public NewsAdapter obtainAdapter() {
        if (newsAdapter==null){
            newsAdapter=new NewsAdapter(newBeans);
        }
        return newsAdapter;
    }
}
